package com.zhu.service.edu.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhu.commons.utils.R;
import com.zhu.service.edu.feign.OssFeignService;
import com.zhu.service.edu.pojo.EduTeacher;
import com.zhu.service.edu.pojo.vo.TeacherQueryVo;
import com.zhu.service.edu.service.IEduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.jsqlparser.statement.create.table.CreateTable;
import org.aopalliance.intercept.Interceptor;
import org.apache.commons.codec.language.bm.Lang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author zhu
 * @since 2022-07-04
 */
@RestController
@RequestMapping("/admin/edu/teacher")
@Api(tags = "教师管理模块")
//@CrossOrigin

public class EduTeacherController {

     @Autowired
     private IEduTeacherService  teacherService;


     @GetMapping("list")
     @ApiOperation(value = "教师列表")
     public R listAll(){
         List<EduTeacher> list = teacherService.list();
         return R.ok().data("items",list);
     }


     @DeleteMapping("/removeById/{id}")
     @ApiOperation(value = "根据id逻辑删除教师")
     public R removeById(@PathVariable("id") String id){

        if(teacherService.removeTeacherById(id)){
            return R.ok().message("删除成功！");
        }
        return R.error().message("数据不存在，删除失败！");

     }

    @DeleteMapping("/batch-remove")
    @ApiOperation(value = "根据id逻辑删除教师")
    public R removeRows(@RequestBody List<String> id){

        if(teacherService.removeByIds(id)){
            return R.ok().message("删除成功！");
        }
        return R.error().message("数据不存在，删除失败！");
    }



     @ApiOperation(value = "分页查询教师")
     @GetMapping("/list/{page}/{limit}")
     public R listPage(@PathVariable(value = "page",required = true) Long page,
                       @PathVariable(value = "limit",required = true) Long limit,
                       TeacherQueryVo teacherQueryVo){
         Page<EduTeacher> eduTeacherPage = new Page<>(page,limit);
         IPage<EduTeacher>  teacherIPage= teacherService.selectPage(eduTeacherPage,teacherQueryVo);
         List<EduTeacher> records = teacherIPage.getRecords();
         long total = teacherIPage.getTotal();
         return R.ok().data("total",total).data("rows",records);
     }

     @ApiOperation(value = "save教师")
     @PostMapping("/save")
     public R save(@RequestBody EduTeacher teacher){
          if(teacherService.save(teacher)){
              return R.ok().message("保存成功！");
          }
          return R.error().message("保存失败");
     }

    @ApiOperation(value = "更新教师")
    @PostMapping("/update")
    public R updateById(@RequestBody EduTeacher teacher){
         if(teacherService.updateById(teacher)){
            return R.ok().message("修改成功！");
        }
        return R.error().message("修改失败");
    }

    @ApiOperation(value = "根据id查询教师")
    @PostMapping("/get/{id}")
    public R getById(@PathVariable String id) {
        EduTeacher eduTeacher = teacherService.getById(id);
        if(eduTeacher!=null){
            return R.ok().data("item",eduTeacher);
        }
      return R.error().message("教师不存在");
    }

    @ApiOperation(value = "根据关键字查询将是列表")
    @GetMapping("/get/{key}")
    public R selectNameListByKey(@PathVariable String key) {
        List<Map<String,Object>> nameList=teacherService.selectNameListByKey(key);
        return R.ok().data("nameList",nameList);
    }
    @Autowired
    private OssFeignService ossFeignService;
     @ApiOperation("测试")
     @GetMapping("test")
    public R test()  {
         try {
             Thread.sleep(500);
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
         ossFeignService.test();
         return R.ok();
    }

    @ApiOperation(value = "删除教师以及头像")
    @DeleteMapping("/deleteAavtar")
    public R deleteAvatarByUrl(@RequestBody String id){
         if(teacherService.deleteAvatarByUrl(id)){
             return R.ok().message("删除成功！");
         }
         return R.error().message("删除失败！");
    }
}
